/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import ejb.Invitationejb;
import ejb.Userejb;
import entity.Event;
import entity.Invitation;
import entity.User;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author user
 */

@Named
@RequestScoped

public class Invitationbean {
    
    @Inject
    Userejb userejb;
    
    @Inject
    Eventshowbean eventshowbean;
    
    @Inject
    Invitationejb invitationejb;
    
    private List<User> users;
    
    private String UsernameInvited, EmailInvited;
    
    private List<Invitation> invitationlist;
    
    private Invitation invitation;
    
    private boolean found = false;
    
    public List<User> returnallusers(){
        users = userejb.returnallusers();
        return users;
    }

    public String getUsernameInvited() {
        return UsernameInvited;
    }

    public void setUsernameInvited(String UsernameInvited) {
        this.UsernameInvited = UsernameInvited;
    }

    public String getEmailInvited() {
        return EmailInvited;
    }

    public void setEmailInvited(String EmailInvited) {
        this.EmailInvited = EmailInvited;
    }
    
    public void addInvitationUser(){
        
        invitation.setReceiverUsername(UsernameInvited);
        invitation.setReceiverEmail(EmailInvited);
        
        if (invitationlist==null)
            invitationlist.add(invitation);
        else{
            found=false;
            for(int i=0; i< invitationlist.size() && found==false; i++){
                if(invitationlist.get(i).getReceiverUsername() == invitation.getReceiverUsername()){
                    found=true;
                    System.out.println("invitation already existent");
                }
            }
            if (found == false){
                System.out.println("invitation not existent");
                invitationlist.add(invitation);
            }
        }
        
    }
    
    public String setInvitationUser(){
        
        for(int i=0; i< invitationlist.size(); i++){
            invitationlist.get(i).setDateTime(eventshowbean.getEvent().getStartDateTime());
            invitationlist.get(i).setStatus("Pending");
        }
        
        invitationejb.persistInvitation(invitationlist);
        
        
        return "/s/user/showevent?faces-redirect=true";
        
    }
    
    
    
}
